﻿using System;
using System.Linq;

using SSTU.Model;
using SSTU.ViewModel.Core;

namespace SSTU.ViewModel.Item
{
	/// <summary>
	/// View model for class.
	/// </summary>
	public class ClassItemVM : ViewModelBase
	{
		public const string TimeSpanStringFormat = "hh\\:mm";

		private TimeSpan? _start;
		private TimeSpan? _end;
		private string _roomId;
		private string _subRoomId;
		private string _buildingOrOrganizationId;
		private string _theme;
		private User _lecturer;

		/// <summary>
		/// Initializes a new instance of the <see cref="ClassItemVM"/> class.
		/// </summary>
		public ClassItemVM()
			: this(null, null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ClassItemVM"/> class.
		/// </summary>
		public ClassItemVM(TimeSpan? start, TimeSpan? end, string roomId = null, string subRoomId = null, string buildingId = null, string theme = null, User lecturer = null)
		{
			Start = start;
			End = end;
			RoomId = roomId;
			SubRoomId = subRoomId;
			BuildingOrOrganizationId = buildingId;
			Theme = theme;
			Lecturer = lecturer;
		}

		/// <summary>
		/// Gets or sets time value for start of lesson.
		/// </summary>
		public TimeSpan? Start
		{
			get { return _start; }
			set
			{
				if (value != _start)
				{
					RaisePropertyChanging<TimeSpan?>(() => Start);
					RaisePropertyChanging<string>(() => StartString);
					_start = value;
					RaisePropertyChanged<TimeSpan?>(() => Start);
					RaisePropertyChanged<string>(() => StartString);
				}
			}
		}

		/// <summary>
		/// Gets string value for start of lesson.
		/// </summary>
		public string StartString
		{
			get
			{
				string result = null;
				if (Start.HasValue)
				{
					result = Start.Value.ToString(TimeSpanStringFormat);
				}
				return result;
			}
		}

		/// <summary>
		/// Gets or sets time value for end of lesson.
		/// </summary>
		public TimeSpan? End
		{
			get { return _end; }
			set
			{
				if (value != _end)
				{
					RaisePropertyChanging<TimeSpan?>(() => End);
					RaisePropertyChanging<string>(() => EndString);
					_end = value;
					RaisePropertyChanged<TimeSpan?>(() => End);
					RaisePropertyChanged<string>(() => EndString);
				}
			}
		}

		/// <summary>
		/// Gets string value for end of lesson.
		/// </summary>
		public string EndString
		{
			get
			{
				string result = null;
				if (End.HasValue)
				{
					result = End.Value.ToString(TimeSpanStringFormat);
				}
				return result;
			}
		}

		/// <summary>
		/// Gets or sets identifier of the room, where there should be lesson.
		/// </summary>
		public string RoomId
		{
			get { return _roomId; }
			set
			{
				if (value != _roomId)
				{
					RaisePropertyChanging<string>(() => RoomId);
					RaisePropertyChanging<string>(() => FullRoomId);
					_roomId = value;
					RaisePropertyChanged<string>(() => RoomId);
					RaisePropertyChanged<string>(() => FullRoomId);
				}
			}
		}

		/// <summary>
		/// Gets or sets identifier of the sub room, where there should be lesson.
		/// </summary>
		public string SubRoomId
		{
			get { return _subRoomId; }
			set
			{
				if (value != _subRoomId)
				{
					RaisePropertyChanging<string>(() => SubRoomId);
					RaisePropertyChanging<string>(() => FullRoomId);
					_subRoomId = value;
					RaisePropertyChanged<string>(() => SubRoomId);
					RaisePropertyChanged<string>(() => FullRoomId);
				}
			}
		}
		

		/// <summary>
		/// Gets or sets identifier of the building, where there should be lesson.
		/// </summary>
		public string BuildingOrOrganizationId
		{
			get { return _buildingOrOrganizationId; }
			set
			{
				if (value != _buildingOrOrganizationId)
				{
					RaisePropertyChanging<string>(() => BuildingOrOrganizationId);
					RaisePropertyChanging<string>(() => FullRoomId);
					_buildingOrOrganizationId = value;
					RaisePropertyChanged<string>(() => BuildingOrOrganizationId);
					RaisePropertyChanged<string>(() => FullRoomId);
				}
			}
		}

		/// <summary>
		/// Gets full room id, with building id, organization id and etc, in the UI format.
		/// </summary>
		public string FullRoomId
		{
			get
			{
				string[] parts = new string[] { BuildingOrOrganizationId, RoomId, SubRoomId };
				string fullRoomId = String.Join("/", parts.Where(p => !string.IsNullOrEmpty(p)));
				return fullRoomId;
			}
		}
		
		/// <summary>
		/// Gets or sets theme of the lesson.
		/// </summary>
		public string Theme
		{
			get { return _theme; }
			set
			{
				if (value != _theme)
				{
					RaisePropertyChanging<string>(() => Theme);
					_theme = value;
					RaisePropertyChanged<string>(() => Theme);
				}
			}
		}
		

		/// <summary>
		/// Gets or sets lecturer for the lesson.
		/// </summary>
		public User Lecturer
		{
			get { return _lecturer; }
			set
			{
				if (value != _lecturer)
				{
					RaisePropertyChanging<User>(() => Lecturer);
					_lecturer = value;
					RaisePropertyChanged<User>(() => Lecturer);
				}
			}
		}
		
	}
}
